package kos.robert.animation;

import java.awt.Graphics2D;

public class SimpleObject {

	private int width;
	private int height;
	private float mass = 1;

	private MotionVector V0, V1;

	private static float gravity = 9.8f / 5000;
	private static float torque = 0.0005f;

	// CONSTRUCTOR
	public SimpleObject() {
		this(0, 0, 0, 0);
	}

	// CONSTRUCTOR
	public SimpleObject(float x, float y, int width, int height) {
		V0 = new MotionVector();
		V1 = new MotionVector();
		V1.x = x;
		V1.y = y;

		this.width = width;
		this.height = height;
	}

	// change position
	public void update(long timePassed) {
		// SWAP
		MotionVector temp = V0;
		V0 = V1;
		V1 = temp;

		V1.vx = V0.vx + V0.ax * timePassed;
		V1.vy = V0.vy + V0.ay * timePassed;

		// V1.ax = 0;
		// V1.ay = gravity;
		if (V1.vy > 0) {
			V1.ay = gravity - dragForce(V1.vy) / mass;
		} else if (V1.vy < 0) {
			V1.ay = gravity + dragForce(V1.vy) / mass;
		} else {
			V1.ay = gravity;
		}
		if (V1.vx > 0) {
			V1.ax = -dragForce(V1.vx) / mass;
		} else if (V1.vx < 0) {
			V1.ax = dragForce(V1.vx) / mass;
		} else {
			V1.ax = 0;
		}

		V1.x = V0.x + V0.vx * timePassed + (V0.ax * timePassed * timePassed) / 2f;
		V1.y = V0.y + V0.vy * timePassed + (V0.ay * timePassed * timePassed) / 2f;
	}

	public float dragForce(float XY) {
		float abs = Math.abs(XY);
		return torque * abs;
	}

	// get x position
	public float getX() {
		return V1.x;
	}

	// get y position
	public float getY() {
		return V1.y;
	}

	// set sprite x position
	public void setX(float x) {
		V1.x = x;
	}

	// set sprite y position
	public void setY(float y) {
		V1.y = y;
	}

	// get sprite width
	public int getWidth() {
		return width;
	}

	// get sprite height
	public int getHeight() {
		return height;
	}

	// get horizontal velocity
	public float getVelocityX() {
		return V1.vx;
	}

	// get verical velocity
	public float getVelocityY() {
		return V1.vy;
	}

	// set horizontal velocity
	public void setVelocityX(float vx) {
		V1.vx = vx;
	}

	// set vertical velocity
	public void setVelocityY(float vy) {
		V1.vy = vy;
	}

	public void draw(Graphics2D g) {

	}
}
